/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trash;

import java.io.IOException;
import java.util.Map;
import javax.naming.InitialContext;
import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
import unc.pds.auth.IPDSSecurity;
import unc.pds.auth.SecurityST;
import unc.pds.data.Consts;
import unc.pds.model.UserRemote;
import unc.pds.model.UserRemoteHome;
import unc.pds.service.LoginRemote;
import unc.pds.service.LoginRemoteHome;


/**
 *
 * @author ain
 */
public class PDSLoginModule implements LoginModule {

    private Subject subject;
    private CallbackHandler handler;
    private Map sharedState;
    private Map option;
    private boolean userAuthenticated;
    private String username;
    private Boolean debug;
    private Object caching;

    public void initialize(Subject subject,
            CallbackHandler callbackHandler, Map sharedState, Map options) {
        this.subject = subject;
        this.handler = callbackHandler;
        this.sharedState = sharedState;
        this.option = options;
        this.debug = Boolean.valueOf((String) options.get("debug"));
        this.caching = options.get("caching");
    }

    public boolean login() throws LoginException {

        NameCallback name = new NameCallback("Username:");
        PasswordCallback password = new PasswordCallback("Password", false);
        try {
            handler.handle(new Callback[]{name, password});
        } catch (IOException e) {
            LoginException ex = new LoginException("IO error getting credentials: " + e.getMessage());
            e.initCause(e);
            throw ex;
        } catch (UnsupportedCallbackException e) {
            LoginException ex = new LoginException("UnsupportedCallback: " + e.getMessage());
            e.initCause(e);
            throw ex;
        }

        String usernameText = name.getName();
        String passwordText = String.valueOf(password.getPassword());

        try {
            IPDSSecurity sec = SecurityST.getSecurity(null);
            if (sec.login(usernameText, passwordText)!=null)
                userAuthenticated = true;
        } catch (Exception e) {
            throw new FailedLoginException("PDSLoginModule.login: " + e.getLocalizedMessage());
        }

        if (userAuthenticated) {
            username = usernameText;
            UsernameCredential cred = new UsernameCredential(username);
            subject.getPublicCredentials().add(cred);
            return true;
        } else {
            throw new FailedLoginException("Username/Password for "
                    + usernameText + " incorrect.");
        }
    }

    public boolean commit() throws LoginException {
        if (userAuthenticated) {
            subject.getPrincipals().add(new UserPrincipal(username));
        }
        username = null;
        return true;
    }

    public boolean abort() {
        username = null;
        return true;
    }

    public boolean logout() {
        username = null;
        return true;
    }

    public Subject getSubject() {
        return subject;
    }

    public UserRemote getUser() {
        if (userAuthenticated) {
            try {
                UserRemote user =
                        ((UserRemoteHome) (new InitialContext()).lookup("ejb/User")).create(username,null);
                return user;
            } catch (Exception e) {
                if (Consts.DEBUG_MODE) System.out.println("" + e.getLocalizedMessage());
            }
        }
        return null;
    }
}
